/**
 * Created by lyc on 16/3/30.
 */


var index = 0;
var askCode;
$(document).ready(function(){

    $('input').iCheck({
        checkboxClass: 'icheckbox_flat-blue',
        radioClass: 'iradio_flat-blue'
    });

    $('input').on('ifChecked', function(event){
        var val=$('input:radio[name="iCheck"]:checked').val();
        if(val == "Other")
        {
            console.log(val);
            $("#otherAnswer").removeAttr("disabled");
        }else{

            $("#otherAnswer").attr("disabled",true).val("");
            console.log(val);
        }
    });
    var idx = 1;
    $('#index').val(idx);
    var url = "http://139.129.25.12/answer";
    $("#submitAnswer").click(function () {
        var selectAnswer=$('input:radio[name="iCheck"]:checked').val();
        var moreAnswer=$("#otherAnswer").val();
        var askCode=$('#askCode').val();
        var stuname=$('#stuname').val();
        var stunum=$('#stunum').val();
        var indexSelect=$('#index').val();
        if(selectAnswer == "Other")
        {
            selectAnswer = moreAnswer;
        }
        htmlobj=$.ajax({
            type: "POST",
            url: url,
            data: { askCode:askCode,
                    stuname: stuname,
                    index: indexSelect,
                    stunum:stunum,
                    answer: selectAnswer,
                    moreAnswer: moreAnswer
            }, success: function(msg) {
                    if(msg.state == 1)
                    {
                        console.log(msg);
                        idx = msg.correct.index;
                        idx++;
                        $("#stunum").attr("disabled",true);
                        $("#stuname").attr("disabled",true);
                        $('#index').val(idx);
                        $('#showcode').text(askCode);
                        answerResult = "你的答案为: " + msg.correct.answer;
                        $('#alterLabel').text('提示');
                        $('#alterInfo').text(answerResult);
                        $("#myModal").modal('show');
                    }else {
                        $('#alterLabel').text('出错啦');
                        $('#alterInfo').text(msg.error);
                        $("#myModal").modal('show');
                    }
                //$("#showCode").text("Code:"+askCode);
            },error: function (msg) {
                $('#alterLabel').text('出错啦');
                $('#alterInfo').text("鞭打网络中");
                $("#myModal").modal('show');
            }
        });

    });

});

